package dynamicGui;

 


import gamePlay.Game;
import gamePlay.Lobby;
import gamePlay.MultiPlayerTest;


import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
import java.util.Map;

import javax.swing.*;

import netcode.client.Client;
import netcode.helper.NetcodeHelper;
import netcode.server.NetworkGame;

/*
 * Author: Enzo
 * Constructors: 
 * 		Lobby object is taken in access the Map<Integer,Game> allGames
 * 
 * Parameters:
 * 		Client object used to access the methods during button pushes
 * 
 * Methods:
 * Private-
 * 	addGamesToLobbyPanel
 * 	addAGameToLobby
 * 	addRefreshButton
 */



public class LobbySelectionMenuE extends JPanel {
	private Lobby lobby_ = MenuCyclingE.client_.getLobby();

	
	/*
	 * Begin Constructors---------------------------------------------------
	 */
	//Default Constructor
	//Parameter Lobby is used to get the Map<Integer,Game> allGames;
    public LobbySelectionMenuE() {
        super();
        

        this.setLayout(new GridLayout(0,1, 0, 10));
        addGamesToLobbyPanel();
        
        addRefreshButton(this);
        addCreateGameButton(this);
        addExitButton(this);
        addFAQButton(this);
    }
    /*
	 * End Constructors------------------------------------------------------------------------------------------------
	 */
    
    
    
    
    
    
    
    
    /*Begin button Creation Methods---------------------------------------------------------------------------------
     * 
     */
     
    //Call this to add All games available
    private void addGamesToLobbyPanel() {
    	
    	//create a separate panel for just the lobby
    	JPanel lobbyPanel = new JPanel();
    	lobbyPanel.setLayout(new GridLayout(0,3, 0, 10));
    	
        //use an iterator to read in all the lobby object, creates labels and buttons
    	//lobby.class uses Map<Integer,Game>
		Iterator<Integer> gameIterator = lobby_.getAllGames().keySet().iterator();
		while(gameIterator.hasNext()){
			int gameID = gameIterator.next();
			String gameName = lobby_.getAllGames().get(gameID).getName();
			String gameState = "";
//			lobby_.getAllGames().get(gameID).getGameState() == NetworkGame.WAITING_TO_START
					
			if(lobby_.getAllGames().get(gameID).getGameState() == NetworkGame.WAITING_TO_START){
				gameState = "Status: " +"Waiting for players";
			}else if(lobby_.getAllGames().get(gameID).getGameState()== NetworkGame.STARTED){
				gameState = "Status: " +"Started";
			}					
					
			//creates a label and button for every game
			addAGameToLobby(lobbyPanel, gameName, gameID, gameState);
		}
    	
		this.add(lobbyPanel);
        
        
    }
    
    
    //ADD in game
    //adds in two a text field for game and a join button for that game
    private void addAGameToLobby(JPanel jpanelIn, String gameNameIn, final int gameCodeIn, String gameStateIn){
    	
    	//create a gameName text field for the game name and add it
    	JLabel lobbyGameNameTextField = new JLabel("|"+gameNameIn+"| " +"GameID: " +gameCodeIn);
    	lobbyGameNameTextField.setPreferredSize(new Dimension(400, 30));
    	jpanelIn.add(lobbyGameNameTextField);
    
    	//create a text field of the current game state
    	JLabel gameStateTextField = new JLabel(gameStateIn);
    	gameStateTextField.setPreferredSize(new Dimension(200, 30));
    	jpanelIn.add(gameStateTextField);
    	
        //create the join button and add it
        JButton joinButton = new JButton("Join Game", null);
        joinButton.setPreferredSize(new Dimension(200, 30));
        jpanelIn.add(joinButton);
    	joinButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	joinButtonActionPerformed(evt, gameCodeIn);
            }
        });
    	
//        System.out.println("DEBUG: created and added a game");//debug
    }
    
    //ADD in a refresh button
    private void addRefreshButton(JPanel jpanelIn) {
    	JButton refreshButton = new JButton("REFRESH");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	jpanelIn.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	refreshButtonActionPerformed(evt);
            }
        });
	}
  //ADD in a exit button
    private void addExitButton(JPanel jpanelIn) {
    	JButton exitButton = new JButton("Exit LobbyScreen");
    	exitButton.setPreferredSize(new Dimension(200, 30));
    	jpanelIn.add(exitButton);
    	exitButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	exitButtonActionPerformed(evt);
            }
        });
	}
  //ADD in a CreateGame button
    private void addCreateGameButton(JPanel jpanelIn) {
    	JButton createGameButton = new JButton("Create Game");
    	createGameButton.setPreferredSize(new Dimension(200, 30));
    	jpanelIn.add(createGameButton);
    	createGameButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	createGameButtonActionPerformed(evt);
            }
        });
	}
	//Add FAQButton
	private void addFAQButton(JPanel jpanelIn){
		JButton refreshButton = new JButton("FAQ");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	jpanelIn.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	FAQButtonActionPerformed(evt);
            }
        });
	}
    /*
     * End button Creation Methods------------------------------------------------------------------------------------------
     */
    
    
    /*
     *ActionListeners begin--------------------------------------------------------------------------------------------------
     */
 
    //Pressing the join button, it joins a certain game based on the gameCode associated with it
    private void joinButtonActionPerformed(java.awt.event.ActionEvent evt, int in_gameCode) {
    	System.out.println("DEBUG: joining Game: " +in_gameCode);
    	
		MenuCyclingE.displayLobbyMenuGuest(in_gameCode);
    }
    
    //Pressing the refresh button, deletes the current gui and recreates it
    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	System.out.println("DEBUG: refreshing");
    	MenuCyclingE.displayLobbySelectionMenu();
     }
    
    //Pressing the Exit button, 
    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	System.out.println("DEBUG: Exit button pressed, change screen to StartingMenu");
    	MenuCyclingE.displayStartingMenu();
    }
    
    //Pressing the create game button, 
    private void createGameButtonActionPerformed(java.awt.event.ActionEvent evt) {
    	System.out.println("DEBUG: CreateGame button pressed, change screen to LobbyMenuHost");
    	MenuCyclingE.displayLobbyMenuHost();
     }
    
    
	//Pressing FAQ Button
	private void FAQButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuickGameButtonActionPerformed
		try {
	        //Set your page url in this string. For eg, I m using URL for Google Search engine
	        String url = "http://homepages.uwp.edu/dalby001/faq.html";
	        java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
	      }
	      catch (java.io.IOException e) {
	          System.out.println(e.getMessage());
	      }
	}
    /*
     *ActionListeners End--------------------------------------------------------------------------------------------------
     */

}
